<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('表格传值给父页面')"/>
</head>
<body class="gray-bg">
<input type="hidden" id="rowIds">
<input type="hidden" id="databaseId">
<input type="hidden" id="databaseName">
<input type="hidden" id="databaseDbname">
<input type="hidden" id="databaseType">
<input type="hidden" id="databaseHost">
<input type="hidden" id="databaseUser">
<input type="hidden" id="databasePassword">
<input type="hidden" id="databaseStatus">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>库名称：</label>
                            <input type="text" name="databaseName" placeholder="请输入用户库名称"/>
                        </li>
                        <li>
                            <label>类型：</label>
                            <select name="databaseType" th:with="type=${@dict.getType('database_type')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="status" th:with="type=${@dict.getType('database_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>主机：</label>
                            <input type="text" name="databaseHost" placeholder="请输入主机名(IP/域名)"/>
                        </li>
                        <li>
                            <label>端口：</label>
                            <input type="text" name="databasePort" placeholder="请输入端口(1-65535)"/>
                        </li>
                        <li>
                            <label>库：</label>
                            <input type="text" name="databaseDbname" placeholder="库名称"/>
                        </li>
                        <li>
                            <label>用户名：</label>
                            <input type="text" name="databaseUser" placeholder="请输入用户名"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var prefix = ctx + "datamonitor/database";
    var databaseTypeDatas = [[${@dict.getType('database_type')}]];
    var databaseStatusDatas = [[${@dict.getType('database_status')}]];

    $(function () {
        var options = {
            url: prefix + "/popuplist",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            columns: [{
                checkbox: true
            },
                {
                    field: 'databaseId',
                    title: '',
                    visible: false
                },
                {
                    field: 'databaseName',
                    title: '库名称'
                },
                {
                    field: 'databaseType',
                    title: '类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(databaseTypeDatas, value);
                    }
                },
                {
                    field: 'databaseHost',
                    title: '主机'
                },
                {
                    field: 'databasePort',
                    title: '端口'
                },
                {
                    field: 'databaseDbname',
                    title: '库'
                },
                {
                    field: 'databaseUser',
                    title: '用户名'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(databaseStatusDatas, value);
                    }
                }]
        };
        $.table.init(options);
    });

    /* 添加用户-选择用户-提交(子页面调用父页面形式) */
    function submitHandler(index, layero) {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.close();
        // 父页面的方法
        // activeWindow().selectUsers();
        // 父页面的变量
        activeWindow().$('#userids').html('我是通过方式一来的：' + rows.join());
    }

    /* 添加用户-选择用户-提交(回调形式-父页面调用子页面) */
    function getSelections() {
        return $.table.selectFirstColumns();
    }

    $("#bootstrap-table").on("check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table", function (e, rowsAfter, rowsBefore) {
        var rows = $.common.equals("uncheck-all", e.type) ? rowsBefore : rowsAfter;
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        var rowIds = $.table.affectedRowIds(rows);
        $("#rowIds").val(rowIds);
        $("#databaseId").val(rows.databaseId);
        $("#databaseName").val(rows.databaseName);
        $("#databaseDbname").val(rows.databaseDbname);
        $("#databaseType").val(rows.databaseType);
        $("#databaseHost").val(rows.databaseHost);
    });
</script>
</body>
</html>